﻿@{
    Layout = "~/Areas/Web/Views/Shared/_UserCenter.cshtml";
    ViewBag.Title = "退换货申请";
    long MaxRGDNumber = (long)ViewBag.MaxRGDNumber;
    decimal MaxRefundAmount = (decimal)ViewBag.MaxRefundAmount;
    Himall.Model.OrderInfo OrderData = (Himall.Model.OrderInfo)ViewBag.OrderInfo;
    long? OrderItemId = (long?)ViewBag.OrderItemId;
    int RefundType = 0;
    bool isRefundOrder = (OrderItemId == null);    //是否为整笔订单退款
    if (isRefundOrder)
    {
        RefundType = 1;
    }
}
@model Himall.Model.OrderItemInfo


<div class="box1 lh24">
    <div class="border-box">
        <table class="tb-void tb-line">
            @if (isRefundOrder)
        {
            <colgroup>
                <col width="400" />
                <col width="200" />
                <col width="100" />
                <col width="100" />
                <col width="100" />
            </colgroup>
            <thead>
                <tr class="tr">
                    <th>订单编号</th>
                    <th>下单时间</th>
                    <th>运费</th>
                    <th>实付金额</th>
                    <th>可退金额</th>
                </tr>
            </thead>
        }
        else
        {
            <colgroup>
                <col width="500" />
                <col width="100" />
                <col width="100" />
            </colgroup>
            <thead>
                <tr class="tr">
                        <th>商品</th>
                        <th>购买数量</th>
                        <th>实付金额</th>
                </tr>
            </thead>
                    }
            <tbody>
                <tr class="tr-td">
                    @if (isRefundOrder)
                    {
                        <td><a target="_blank" href="@Url.Action("Detail", "UserOrder", new { id=OrderData.Id})">@(OrderData.Id)</a></td>
                        <td>@(OrderData.OrderDate.ToString("yyyy-MM-dd HH:mm:ss"))</td>
                        <td>￥@(OrderData.Freight.ToString("F2"))</td>
                        <td>￥@((OrderData.ProductTotalAmount-OrderData.DiscountAmount).ToString("F2"))</td>
                        <td class="ftx-04">￥@OrderData.OrderEnabledRefundAmount.ToString("F2")</td>
                    }
                    else {
                        var spc = "";
                        if (!string.IsNullOrWhiteSpace(Model.Color)) { spc += "颜色：" + Model.Color; }
                        if (!string.IsNullOrWhiteSpace(Model.Size)) { spc += "，尺寸：" + Model.Size; }
                        if (!string.IsNullOrWhiteSpace(Model.Version)) { spc += "，版本：" + Model.Version; }
                        spc=spc.TrimStart('，');
                        spc = string.IsNullOrWhiteSpace(spc) ? "" : "【" + spc + "】";
                        <td class="t-l"><a target="_blank" href="/Product/Detail/@Model.ProductId"><img width="50" height="50" title="" src="@Model.ThumbnailsUrl" />@Model.ProductName @spc
                           </a></td>
                        <td>@(Model.Quantity)件</td>
                        <td class="ftx-04">￥@Model.RealTotalPrice.ToString("F2")</td>
                    }
                </tr>
            </tbody>

        </table>
    </div>
    <div class="title bot-border mt20">
        <h3 class="title_txt curr">售后申请</h3>
    </div>
    <input type="hidden" name="maxNumber" value="@(MaxRGDNumber)">
    <input type="hidden" name="maxPrice" value="@(MaxRefundAmount.ToString("F2"))">
    <div class="border-box">
        <div class="user-set userset-lcol">
            <div class="form">
                <form id="form1" action="@Url.Action("RefundApply")" method="post">
                    <input type="hidden" name="OrderId" value="@(OrderData.Id)" />
                    <input type="hidden" name="OrderItemId" value="@(OrderItemId)">
                    <div class="item">
                        <span class="label"><em>*</em>售后类型：</span>
                        <div class="fl">
                            <label>@Html.RadioButton("RefundType", 1, RefundType==1) 仅退款</label>
                            @if(!isRefundOrder){<label>@Html.RadioButton("RefundType", 2, RefundType==2) 退货退款</label>}
                        </div>
                    </div>
                    <div class="item" id="ReturnGoodsBox">
                        <span class="label"><em>*</em>退货数量：</span>
                        <div class="fl">
                            @Html.TextBox("ReturnQuantity", MaxRGDNumber , new Dictionary<string, object>() {
                       { "class", "input-item" },
                       {"maxlength","20"},
                       { "data-check", "0" } }) 件
                            <span class="ftx-03" id="ReturnQuantity">（最多@(MaxRGDNumber)件）</span>
                        </div>
                    </div>
                    <div class="item">
                        <span class="label">退款金额：</span>
                        <div class="fl">
                            @Html.TextBox("Amount", MaxRefundAmount.ToString("F2") , new Dictionary<string, object>() {
                       { "class", "input-item" },
                       {"maxlength","20"},
                       { "data-check", "0" } }) 元
                            <span class="ftx-03" id="Amount">（最多@(MaxRefundAmount.ToString("F2"))元）</span>
                        </div>
                    </div>
                    <div class="item">
                        <span class="label"><em>*</em>退款理由：</span>
                        <div class="fl">
                            @Html.TextArea("Reason", (OrderItemId == null ? "取消订单退款" : ""), new Dictionary<string, object>() {
                       { "class", "textarea-item" },
                       {"cols","60"},
                       {"rows","5"},
                       { "data-check", "0" } })
                        </div>
                    </div>
                    <div class="item">
                        <span class="label"><em>*</em>联系人：</span>
                        <div class="fl">
                            @Html.TextBox("ContactPerson", (string)ViewBag.UserName , new Dictionary<string, object>() {
                       { "class", "input-item" },
                       {"maxlength","20"},
                       { "data-check", "0" } })
                        </div>
                    </div>
                    <div class="item">
                        <span class="label"><em>*</em>联系电话：</span>
                        <div class="fl">
                            @Html.TextBox("ContactCellPhone", (string)ViewBag.Phone , new Dictionary<string, object>() {
                       { "class", "input-item" },
                       {"maxlength","20"},
                       { "data-check", "0" } })
                        </div>
                    </div>
                    <div class="item">
                        <span class="label"><em>*</em>退款方式：</span>
                        <div class="fl">
                            @Html.DropDownList("RefundPayType",(IEnumerable<SelectListItem>) ViewBag.RefundWay, new { @class = "form-control input-sm" })
                        </div>
                    </div>

                    <div class="item">
                        <span class="label">&nbsp;</span>
                        <div class="fl">
                            <input type="submit" class="btn-5" id="bt-submit" value="提交" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="~/Scripts/v.js"></script>
<script type="text/javascript">
    var isLoading = false;

    $(function () { $("#Reason").focus(); });
    var maxNumber = $(':input[name=maxNumber]').val(),
        maxPrice = $(':input[name=maxPrice]').val();
    var loadingobj;
    var a = v({
        form: 'form1',
        beforeSubmit:function(){
            if (!$("input[name=RefundType]:checked").val()) {
                $.dialog.errorTips("请选择售后类型", '', 3);
                return false;
            }
            var a = $('#Reason').val();
            if (a.length < 6) {
                $.dialog.errorTips("请填写退款原因，不低于6字符", '', 3);
                return false;
            }
            //var a = $('#RefundAccount').val();
            //if (a.length < 3) {
            //    $.dialog.errorTips("请填写转款方式/银行", '', 3);
            //    return false;
            //}
            if (isLoading) { return false; }
            loadingobj = showLoading();
            isLoading = true;
            $("#bt-submit").val("提交中...");
        },
        ajaxSubmit: true,
        afterSubmit: function (data) {// 表单提交成功回调           
            isLoading = false;
            loadingobj.close();
            $("#bt-submit").val("提交");
            if (data.success) {
                $.dialog.succeedTips("提交成功！", function () {
                    window.location.href = "/OrderRefund/Detail/" + data.id;
                }, 3);
            } else {
                $.dialog.errorTips(data.msg, '', 3);
            }
        }
    });
    a.add(
        {
            target: 'Amount',
            ruleType: 'money',
            fnRule: function () {
                var a = $('#Amount').val();
                if ((+a) > (+maxPrice)) {
                    return false;
                }
            },
            error: '不能超过' + maxPrice + '元!'
        },
        {
            target: 'Reason',
            ruleType: 'required',
            beforeBlur: function () {
                var a = $('#Reason').val();
                if (a.length < 6 || a.length>500) {
                    return false;
                }
            },
            error: '请输入6 - 500字符!'
        },
        {
            target: 'ContactPerson',
            //ruleType: 'ce&&required',
            rule:/^[\u4E00-\u9FA5A-Za-z0-9_\ ]{1,20}$/i,
            error: '名字不能包含非法字符!'
        },
        {
            target: 'ContactCellPhone',
            ruleType: 'mobile||phone',
            error: '请输入正确的电话号码!'
        }
    );
    $("input[name='RefundType']").click(function () {
        var _t = $(this);
        rtype = _t.val();
        ChangeRefundType(rtype);
    });

    function ChangeRefundType(v)
    {
        var rgbox = $("#ReturnGoodsBox");
        if (v == 2) {
            rgbox.show();
            a.add({
                target: 'ReturnQuantity',
                ruleType: 'uint',// v.js规则验证
                fnRule: function () {// 增加函数验证来补充规则验证的不足 库默认规则验证优先
                    var a = $('#ReturnQuantity').val();
                    if ((+a) > (+maxNumber)) {
                        return false;
                    }
                },
                error: '不能超过' + maxNumber + '件或者只能为数字!'
            });
        } else {
            rgbox.hide();
            a.remove('ReturnQuantity');
        }

    }
    ChangeRefundType(@(RefundType));
</script>